import * as THREE from "three";
import { GUI } from "three/examples/jsm/libs/lil-gui.module.min.js";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls"; // 控制器-控制相机运行轨道
import { RoomEnvironment } from "three/examples/jsm/environments/RoomEnvironment.js";
import Stats from "three/examples/jsm/libs/stats.module";
import * as Listener from "./EventListen";
// import * as Mesh from './Cube'
import * as model01 from "./Model01";

var scene;
var camera;
var renderer;
var controls;
var stats; // 性能监控
var width = 1053;
var height = 473;
var debug;

function init(ref) {
  width = window.innerWidth;
  height = window.innerHeight;

  setDebug();
  setScene();
  serCamera();

  // Mesh.init(scene,debug)
  model01.init(scene,renderer);
  setHelper();
  setStats(ref);
  setRender(ref);
  setControl();
  setListener();

  animation();
}

function setScene() {
  scene = new THREE.Scene(); // 创建场景
  scene.background = new THREE.Color(0xffffff);

//   const light = new THREE.AmbientLight(0xffffff); // 柔和的白光
//   scene.add(light);

//   const folder = debug.addFolder("glows");
//   folder.addColor(light, "color");
//   console.log("scene", scene);
}

function setListener() {
  Listener.init(scene, renderer, camera);
  Listener.addListener(visibilitychange);
}

function serCamera(w = width, h = height) {
  camera = new THREE.PerspectiveCamera(60, w / h, 1, 2000); // 创建相机
  camera.position.set(5, 5, 5); // 设置相机位置
  camera.lookAt(0, 0, 0); // 设置相机望向的位置
}

function setRender(ref, w = width, h = height) {
  renderer = new THREE.WebGLRenderer({ antialias: true }); // 创建渲染器
  renderer.setSize(w, h); //设置渲染器尺寸
  renderer.setPixelRatio( window.devicePixelRatio );
  // https://juejin.cn/post/7109648309509160973
  renderer.outputEncoding = THREE.sRGBEncoding; 

  console.log("ref", ref);
  ref.appendChild(renderer.domElement);

  const pmremGenerator = new THREE.PMREMGenerator(renderer);
  scene.environment = pmremGenerator.fromScene(new RoomEnvironment(), 0.04).texture;

  renderer.render(scene, camera);
}

function setHelper() {
  const gridHelperName = new THREE.GridHelper(20, 10);
  const axeshelper = new THREE.AxesHelper(500);
  scene.add(axeshelper);
  scene.add(gridHelperName);
}

function setDebug() {
  debug = new GUI();
}

function setControl() {
  controls = new OrbitControls(camera, renderer.domElement);
  controls.target.set(0, 0, 0);
  // controls.autoRotate = true
  controls.enableDamping = true;
}

function setStats(ref) {
  // 性能监控
  stats = Stats();
  // 添加类自定义显示的位置
  stats.domElement.classList.add("stats-bar");
  ref.appendChild(stats.domElement);
}

var timer;
function animation() {
  stats.update();
  controls.update();
  renderer.render(scene, camera);
  timer = requestAnimationFrame(animation);
  // console.log("动画动画动画");
}

function cancaleAnimation() {
  cancelAnimationFrame(timer);
}

function visibilitychange() {
  if (document.hidden) {
    // 看不见啦
    cancaleAnimation();
  } else {
    // 看得见啦
    animation();
  }
}

function destory() {
  renderer.dispose();
  controls.dispose();
  debug.destroy();

  cancaleAnimation();
  Listener.removeEvent(visibilitychange);
  // 内容销毁
  console.log("内容销毁");
}

export { init, destory };
